package www.dianbaozu.com.ui.manage

import android.text.InputFilter
import android.text.InputFilter.LengthFilter
import android.view.View
import androidx.lifecycle.lifecycleScope
import www.dianbaozu.com.R
import www.dianbaozu.com.extension.requestUniversally
import www.dianbaozu.com.ui.server.ChangingUseNameInServerFragment
import www.wen.com.common.lib.data.entities.RequestBodyParameter
import www.wen.com.common.lib.extension.postObject
import www.wen.com.common.lib.extension.toast
import www.wen.com.common.lib.extension.trimProxy
import www.wen.com.common.lib.helper.loading.KeyHelper


/**
 * @author WEN
 * @Description:修改服务器简介
 * @date 2020//25 17:48
 */
class ChangeServerIntroduceFragment : ChangingUseNameInServerFragment(),
        View.OnClickListener {

    override fun getName(): CharSequence = mServerInfo.serverIntro

    override fun getTitleName() = getString(R.string.server_introduce)

    override fun geRightText() = getString(R.string.com_save)

    override fun getCreateGroupText(): CharSequence = getString(R.string.com_modify_server_introduce)

    override fun getCreateGroupText1(): CharSequence = getString(R.string.please_enter_the_server_introduce)

    override fun getCheckText(): String = getString(R.string.com_server_introduce_empty)

    override fun lazyInitView() {
        super.lazyInitView()
        dataBinding.etPleaseEnterAGroupName.filters = arrayOf<InputFilter>(LengthFilter(40))
    }

    override fun actionRight() {
        if (dataBinding.etPleaseEnterAGroupName.trimProxy().isBlank()) {
            toast(getCheckText())
            return
        }
        mUpdateServerInfoViewModel.requestUniversally(
                viewModelScope = lifecycleScope,
                tipSuccess = "",
                isShowToastTip = false,
                resultFailCall = {
                    mUpdateServerInfoViewModel.mIsShowErrorTip.set(View.VISIBLE)
                    mUpdateServerInfoViewModel.mErrorTipText.set(it)
                },
                request = { appDataManage ->
                    appDataManage.requestUpdateServerInfo(
                            serverId = mServerInfo.serverId,
                            RequestBodyParameter(
                                    serverIntro = dataBinding.etPleaseEnterAGroupName.trimProxy()
                            ))
                }
        ) {
            //更新服务器信息
            postObject(KeyHelper.UPDATE_SERVER_INFORMATION_KEY, it)
            toast("修改服务器简介成功")
            actionLeft()
        }
    }

}





